﻿using AutoMapper;
using FluentValidation;
using Mes.Application.Common;
using Microsoft.AspNetCore.Mvc;
using Mes.Application.DTO.Inputs.MasterData;
using Mes.Application.Services.MasterData;

namespace Mes.Application.Web.Controllers.MasterData
{
    [ApiController]
    [Route("/api/v1")]
    public class MeasuringUnitController : Controller
    {
        private readonly ILogger<MeasuringUnitController> _logger;
        private readonly IMeasuringUnitService _measuringUnitService;
        private readonly IMapper _mapper;
        private readonly IValidator<MeasuringUnitInput> _measuringUnitInputValidator;
        private readonly IValidator<MeasuringUnitUpdateInput> _measuringUnitUpdateInputValidator;
        private readonly IValidator<MeasuringUnitPageInput> _measuringUnitPageInputValidator;

        public MeasuringUnitController(ILogger<MeasuringUnitController> logger,
            IMeasuringUnitService measuringUnitService,
            IMapper mapper,
            IValidator<MeasuringUnitInput> measuringUnitInputValidator,
            IValidator<MeasuringUnitUpdateInput> measuringUnitUpdateInputValidator,
            IValidator<MeasuringUnitPageInput> measuringUnitPageInputValidator
            )
        {
            _logger = logger;
            _measuringUnitService = measuringUnitService;
            _mapper = mapper;
            _measuringUnitInputValidator = measuringUnitInputValidator;
            _measuringUnitUpdateInputValidator = measuringUnitUpdateInputValidator;
            _measuringUnitPageInputValidator = measuringUnitPageInputValidator;
        }

        /// <summary>
        /// 添加计量单位
        /// </summary>
        /// <param name="measuringUnitInput"></param>
        /// <returns></returns>
        [HttpPost("measuringunits")]
        public async Task<ActionResult> AddMeasuringUnit(MeasuringUnitInput measuringUnitInput)
        {
            try
            {
                var input = await _measuringUnitInputValidator.ValidateAsync(measuringUnitInput);
                if (!input.IsValid)
                {
                    return BadRequest(input.Errors.First().ErrorMessage);
                }

                await _measuringUnitService.AddMeasuringUnitAsync(measuringUnitInput);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 修改计量单位
        /// </summary>
        /// <param name="measuringUnitUpdateInput"></param>
        /// <returns></returns>
        [HttpPut("measuringunits")]
        public async Task<ActionResult> UpdateMeasuringUnit(MeasuringUnitUpdateInput measuringUnitUpdateInput)
        {
            try
            {
                var input = await _measuringUnitUpdateInputValidator.ValidateAsync(measuringUnitUpdateInput);
                if (!input.IsValid)
                {
                    return BadRequest(input.Errors.First().ErrorMessage);
                }

                await _measuringUnitService.UpdateMeasuringUnitAsync(measuringUnitUpdateInput);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 删除计量单位
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete("measuringunits")]
        public async Task<ActionResult> DeleteMeasuringUnit(int[] ids)
        {
            try
            {
                await _measuringUnitService.DeleteMeasuringUnitAsync(ids);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 分页查询计量单位
        /// </summary>
        /// <param name="measuringUnitPageInput"></param>
        /// <returns>PagedItemsData<MeasuringUnitResult></returns>
        [HttpGet("measuringunits")]
        public async Task<ActionResult> GetMeasuringUnit([FromQuery] MeasuringUnitPageInput measuringUnitPageInput)
        {
            try
            {
                var input = await _measuringUnitPageInputValidator.ValidateAsync(measuringUnitPageInput);
                if (!input.IsValid)
                {
                    return BadRequest(input.Errors.First().ErrorMessage);
                }

                var result = await _measuringUnitService.GetMeasuringUnitsAsync(measuringUnitPageInput);
                return Ok(result);
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 根据主键编号查询计量单位
        /// </summary>
        /// <param name="id"></param>
        /// <returns>MeasuringUnitResult</returns>
        [HttpGet("measuringunits/entity")]
        public async Task<ActionResult> GetMeasureingUnitEntityById(int id)
        {
            try
            {
                var result = await _measuringUnitService.GetMeasuringUnitAsync(id);
                return Ok(result);
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }
    }
}
